package com.base.service.impl;

import com.base.dto.MoneyGoldWebListDTO;
import com.base.mapper.voipmapper.MoneyGoldMapper;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.param.NetState;
import com.base.po.voip.MoneyGold;
import com.base.po.voip.MoneyGoldExample;
import com.base.po.voip.Subscribers;
import com.base.self.common.PageUtil;
import com.base.service.MoneyGoldService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service("MoneyGoldService")
public class MoneyGoldServiceImpl implements MoneyGoldService {

    private final MoneyGoldMapper moneyGoldMapper;
    private final SubscribersMapper subscribersMapper;

    public MoneyGoldServiceImpl(MoneyGoldMapper moneyGoldMapper, SubscribersMapper subscribersMapper) {
        this.moneyGoldMapper = moneyGoldMapper;
        this.subscribersMapper = subscribersMapper;
    }

    @Override
    public BigDecimal getGoldMoneyByUserId(String userId, Integer statement) {
        BigDecimal add = moneyGoldMapper.getGoldMoneyByUserId(userId, statement, 1);
        BigDecimal del = moneyGoldMapper.getGoldMoneyByUserId(userId, statement, 0);
        return add.subtract(del);
    }

    @Override
    public Page<MoneyGold> getGoldMoneyDetailByUserId(String userId, Integer statement, Integer pageNo, Integer pageSize) {
        Page<MoneyGold> page = PageHelper.startPage(pageNo, pageSize).setOrderBy("gold_time desc");
        List<MoneyGold> moneyGoldList = moneyGoldMapper.getGoldMoneyDetailByUserId(userId, statement);
        return page;
    }


    @Override
    public int countByExample(MoneyGoldExample example) {
        return moneyGoldMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(MoneyGoldExample example) {
        return moneyGoldMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return moneyGoldMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(MoneyGold record) {
        return moneyGoldMapper.insert(record);
    }

    @Override
    public int insertSelective(MoneyGold record) {
        return moneyGoldMapper.insertSelective(record);
    }

    @Override
    public List<MoneyGold> selectByExample(MoneyGoldExample example) {
        return moneyGoldMapper.selectByExample(example);
    }

    @Override
    public MoneyGold selectByPrimaryKey(Integer id) {
        return moneyGoldMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(MoneyGold record, MoneyGoldExample example) {
        return moneyGoldMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(MoneyGold record, MoneyGoldExample example) {
        return moneyGoldMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(MoneyGold record) {
        return moneyGoldMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MoneyGold record) {
        return moneyGoldMapper.updateByPrimaryKey(record);
    }

    @Override
    public NetState moneyGoldWebList(MoneyGoldWebListDTO moneyGoldWebListDTO) {
        final Map<String, Object> item = new HashMap<>();
        if (moneyGoldWebListDTO.getMobile() == null) {
            item.put("user", null);
        } else {
            item.put("user", Optional.ofNullable(subscribersMapper.selectBySubscribers(moneyGoldWebListDTO.getMobile(), null)).orElse(new Subscribers()).getId());
        }
        item.put("status", moneyGoldWebListDTO.getStatus());
        Page<MoneyGold> page = PageHelper.startPage(moneyGoldWebListDTO.getOffset(), moneyGoldWebListDTO.getLimit());
        List<MoneyGold> data = moneyGoldMapper.selectListWeb(item);
        page.getResult().forEach(item2 -> {
            item2.setGoldUser(Optional.ofNullable(subscribersMapper.selectByPrimaryKey(item2.getGoldUser())).orElse(new Subscribers()).getMobile());
        });
        return new NetState("查询成功", NetState.SUCCESS_CONN, PageUtil.getMap(page.getTotal(), data));
    }

    @Override
    public NetState transfer(String userId, String sharePerId, BigDecimal money) {
        Subscribers sub = subscribersMapper.selectByPrimaryKey(userId);
        Subscribers sharePer = subscribersMapper.selectByPrimaryKey(sharePerId);
        MoneyGold moneyGoldAdd = new MoneyGold(null, userId, sharePerId, "转账给:" + sharePer.getMobile() + "扣除", money, 0, 1, new Date());
        MoneyGold moneyGoldDel = new MoneyGold(null, sharePerId, userId, "从" + sub.getMobile() + "转账获取", money, 1, 1, new Date());
        moneyGoldMapper.insertSelective(moneyGoldAdd);
        moneyGoldMapper.insertSelective(moneyGoldDel);
        return new NetState(NetState.SUCCESS_CONN, "转账成功");
    }

    @Override
    public BigDecimal selectAllMoneyByLike(String txUserId, String str) {
        BigDecimal moenyadd = moneyGoldMapper.countMoneyUserLike(txUserId, 1, str);
        BigDecimal moneydel = moneyGoldMapper.countMoneyUserLike(txUserId, 0, str);
        return moenyadd.subtract(moneydel).setScale(2, RoundingMode.HALF_DOWN);
    }
}
